package com.freeter.modules.HelpCateDetail.controller;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Base64;
import java.util.List;

import com.freeter.common.validator.ValidatorUtils;
import com.freeter.datasources.DataSourceNames;
import com.freeter.datasources.annotation.DataSource;
import com.freeter.modules.HelpCategory.entity.HelpCategoryEntity;
import com.freeter.modules.HelpCategory.service.HelpCategoryService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.freeter.common.utils.JQPageInfo;
import com.freeter.common.mpextend.parser.ParseWrapper;
import com.freeter.common.utils.PageInfo;


import com.freeter.modules.HelpCateDetail.entity.HelpCateDetailEntity;
import com.freeter.modules.HelpCateDetail.entity.view.HelpCateDetailView;
import com.freeter.modules.HelpCateDetail.entity.model.HelpCateDetailModel;

import com.freeter.modules.HelpCateDetail.service.HelpCateDetailService;
import com.freeter.common.utils.PageUtils;
import com.freeter.common.utils.R;
import com.freeter.common.utils.MPUtil;



/**
 * 站点帮助分类详情
 * 后端接口
 * @author xuchen
 * @email 171998110@qq.com
 * @date 2019-09-10 13:41:33
 */
@RestController
@RequestMapping("HelpCateDetail/helpcatedetail")
@SuppressWarnings({"unchecked","rawtypes"})
public class HelpCateDetailController {
    @Autowired
    private HelpCateDetailService helpCateDetailService;
    @Autowired
    private HelpCategoryService helpCategoryService;
    /**
     * 列表
     */
    @RequestMapping("/page")
    @RequiresPermissions("HelpCateDetail:helpcatedetail:list")
    public PageUtils page(JQPageInfo jqPageInfo,HelpCateDetailModel helpCateDetail){
        EntityWrapper< HelpCateDetailEntity> ew = ParseWrapper.parseWrapper(helpCateDetail);
     	PageInfo pageInfo = new PageInfo(jqPageInfo);
     	PageUtils page = helpCateDetailService.queryPage(pageInfo, ew);
        List<?> list = page.getList();
        list.forEach(dt->{
            HelpCateDetailEntity detailEntity=(HelpCateDetailEntity)dt;
            String apply =detailEntity.getApply();
            if ("1".equals(apply)){
                detailEntity.setApplyName("h5应用");
            }else if("0".equals(apply)){
                detailEntity.setApplyName("pc应用");
            }
            Integer helpCateId = detailEntity.getHelpCateId();
            HelpCategoryEntity helpCategoryEntity = helpCategoryService.selectById(helpCateId);
            if (helpCategoryEntity!=null){
                detailEntity.setParentName(helpCategoryEntity.getName());
            }
        });
        return page;
        
    }

	/**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("HelpCateDetail:helpcatedetail:list")
    public R list( HelpCateDetailModel helpCateDetail){
       	EntityWrapper< HelpCateDetailEntity> ew = ParseWrapper.parseWrapper(helpCateDetail);
        return R.ok().put("data",  helpCateDetailService.selectListView(ew));
    }

	 /**
     * 查询
     */
    @RequestMapping("/query")
    @RequiresPermissions("HelpCateDetail:helpcatedetail:info")
    public R query(HelpCateDetailEntity helpCateDetail){
        EntityWrapper< HelpCateDetailEntity> ew = ParseWrapper.parseWrapper(helpCateDetail);
		HelpCateDetailView  helpCateDetailView =  helpCateDetailService.selectView(ew);
		return R.ok("查询站点帮助分类详情成功").put("data",  helpCateDetailView);
    }
	
    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("HelpCateDetail:helpcatedetail:info")
    public R info(@PathVariable("id") Integer id){
        HelpCateDetailEntity helpCateDetail = helpCateDetailService.selectById(id);
        Integer helpCateId = helpCateDetail.getHelpCateId();
        HelpCategoryEntity helpCategoryEntity = helpCategoryService.selectById(helpCateId);
        helpCateDetail.setParentName(helpCategoryEntity.getName());

        return R.ok().put("helpCateDetail", helpCateDetail);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("HelpCateDetail:helpcatedetail:save")
    public R save(@RequestBody HelpCateDetailEntity helpCateDetail)throws Exception{
       String  content =new  String(Base64.getDecoder().decode(helpCateDetail.getContent().getBytes(StandardCharsets.UTF_8)));
    	ValidatorUtils.validateEntity(helpCateDetail);
        helpCateDetail.setContent(content);
        helpCateDetailService.insert(helpCateDetail);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("HelpCateDetail:helpcatedetail:update")
    public R update(@RequestBody HelpCateDetailEntity helpCateDetail){
        ValidatorUtils.validateEntity(helpCateDetail);
        String  content =new  String(Base64.getDecoder().decode(helpCateDetail.getContent().getBytes(StandardCharsets.UTF_8)));
        helpCateDetail.setContent(content);
        helpCateDetailService.updateAllColumnById(helpCateDetail);//全部更新
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("HelpCateDetail:helpcatedetail:delete")
    public R delete(@RequestBody Integer[] ids){
        helpCateDetailService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }

}
